package hotel.service.impl;

import hotel.service.IHotelService;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import common.dao.BedTypeDAO;
import common.dao.HotelDAO;
import common.dao.HotelOrderDAO;
import common.dao.ReviewDAO;
import common.dao.RoomStyleDAO;
import common.pojo.Hotel;
import common.pojo.HotelView;
import common.pojo.Review;
import common.pojo.RoomStyle;


@Scope("prototype")
@Service
public class HotelService implements IHotelService {


	public Hotel findInfo(){
		return hotelDAO.findInfo();
	}


	public boolean update(Hotel hotel){
		hotelDAO.update(hotel);
    	return true;
    }
	
	public HotelView findHotel() {
		Hotel hotel=hotelDAO.findInfo();
		double minPrice= roomStyleDAO.findMinPrice();
		int reviewCount=reviewDAO.getCount(" where status=1");
		String goodComment;
		if(reviewCount>0){
		double good=(double)reviewDAO.getCount(" where comment=1  and status=1")/reviewCount*100;
		goodComment=String.valueOf(good);
		}else{
			goodComment="0";
		}
		HotelView hotelView = new HotelView(hotel, goodComment, minPrice,reviewCount);
		return hotelView; 
	}


	public List<RoomStyle> findRoomList(String where,Timestamp checkinTime,Timestamp checkoutTime) {
		List<RoomStyle> roomlist= roomStyleDAO.findList(where);
		for (int i = 0; i < roomlist.size(); i++) {
			//取房间的房间名称
			roomlist.get(i).setBedTypeName(
					bedTypeDAO.findInfoById(roomlist.get(i).getBedType())
							.getName());
			roomlist.get(i).setIsAvailable(checkRoom(roomlist.get(i).getId(),checkinTime,checkoutTime));
		}
		return roomlist;
	}

	public RoomStyle findRoom(int id, Timestamp checkinTime, Timestamp checkoutTime) {
		RoomStyle room =roomStyleDAO.findInfoById(id);
		room.setBedTypeName(bedTypeDAO.findInfoById(room.getBedType()).getName());
		room.setIsAvailable(checkRoom(id,checkinTime,checkoutTime));
		return room;
	}
	
	//判断房间剩余
	private Boolean checkRoom(int id,Timestamp checkinTime,Timestamp checkoutTime) {
		return getLeftNum(id,checkinTime,checkoutTime)>0?true:false;
	}
	
	//房间剩余数量
	public int getLeftNum(int id,Timestamp checkinTime,Timestamp checkoutTime){
		int total = roomStyleDAO.findInfoById(id).getRoomNum();
		int checkedNum = orderDAO.getCountOrdered("roomStyleId="+id+"and tradeStatus in (1,2,3)  and  (checkinTime <= '"+checkoutTime+"' and checkoutTime > '"+checkinTime+"')");
		return total-checkedNum;
	}

	public double findMinPrice() {
		return roomStyleDAO.findMinPrice();		
	}

	public int getCount() {
		return reviewDAO.getCount("");
	}


	public List<Review> findReviewResult(String where) {
		return reviewDAO.findList(where);
	}
	
	@Resource
	private HotelDAO hotelDAO;
	@Resource
	private RoomStyleDAO roomStyleDAO;
	@Resource
	private ReviewDAO reviewDAO;
	@Resource
	private BedTypeDAO bedTypeDAO;
	@Resource
	private HotelOrderDAO orderDAO;


}
